<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Models\CompanyAuth;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Response;

class CompanyAuthController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        if(!$this->user->can('viewCompanyAuth')) abort(405);
        
        $auths = CompanyAuth::paginate(20);
        return view('admin.enterprise.companyauth.index')->with('auths', $auths);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        if(!$this->user->can('editCompanyAuth')) abort(405);
        
        return view('admin.enterprise.companyauth.add')->with('blademod', 'add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  Request  $request
     * @return Response
     */
    public function store(Request $request)
    {
        if(!$this->user->can('editCompanyAuth')) abort(405);
        
        $auth_id = $request->get('auth_id', 0);
        $post = array_except(Input::all(), ['_token']);
        if ($auth_id > 0) {
            $auth              = CompanyAuth::find($auth_id);
            $auth->name        = $post['name'];
            $auth->description = $post['description'];
            $auth->icon        = $post['icon'];
            $auth->save();
        } else {
            CompanyAuth::firstOrCreate($post);
        }
        return redirect("admin/enterprise/auth_type");
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function iconPath(Request $request)
    {
        if(!$this->user->can('editCompanyAuth')) 
            return $this->formatJson('error',$this->permerror);
        
        $id = $request->get('auth_id', 0);
        $icon_path = CompanyAuth::where(['auth_id' => $id])->pluck('icon');
        return  Response::json(array('status'=>'success','msg'=>'', 'data' => $icon_path ));
    }

    /**
     * 异步获取简介
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function brief(Request $request)
    {
        if(!$this->user->can('viewCompanyAuth'))
            return $this->formatJson('error',$this->permerror);
        
        $id = $request->get('auth_id', 0);
        $description = CompanyAuth::where(['auth_id' => $id])->pluck('description');
        return  Response::json(array('status'=>'success','msg'=>'', 'data' => $description ));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        if(!$this->user->can('viewCompanyAuth')) abort(405);
        
        $auth = CompanyAuth::find($id);
        return view('admin.enterprise.companyauth.add')->with('blademod', 'edit')->with('auth', $auth);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  Request  $request
     * @param  int  $id
     * @return Response
     */
    public function update(Request $request, $id)
    {
        if(!$this->user->can('editCompanyAuth'))
            return $this->formatJson('error',$this->permerror);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        if(!$this->user->can('deleteCompanyAuth'))
            return $this->formatJson('error',$this->permerror);
        
        $auth = CompanyAuth::find($id);
        if ($auth->delete()){
            /*删除图标*/
            $file_path = public_path() . $auth->icon;
            if (file_exists($file_path)) {
                unlink($file_path);
            }
            return  Response::json(array('status'=>'success','msg'=>'认证类别 ['. $auth->name .'] 删除成功! '));
        }
        else
            return  Response::json(array('status'=>'error','msg'=>'删除失败'));
    }
}
